#region

/* Adoor.NET - Object/Relational mapping framework leveraging ADO.NET`s powerful disconnected mode.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#endregion

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;

namespace Adoor.Object.Domain
{
	/// <summary>
	/// The starting point for property metadata.
	/// </summary>
	public class PropertyData
	{
		private EntityData sourceData;
		private string name;
		private PropertyInfo propertyInfo;
		private Adoor.Expression.AST.SelectExpression selectExpression;

		public PropertyData(EntityData sourceData, PropertyInfo propertyInfo, XmlNode metadata)
		{
			this.sourceData = sourceData;
			this.name = metadata.ParentNode.LocalName;
			this.propertyInfo = propertyInfo;

			this.sourceData.RegisterPropertyData(this.name, this);
		}

		public PropertyData(EntityData sourceData, Adoor.Expression.AST.SelectExpression selectExpression)
		{
			this.sourceData = sourceData;
			this.name = selectExpression.Alias;
			this.selectExpression = selectExpression;
			if(this.name == string.Empty)
			{
				if(selectExpression.Expr is Adoor.Expression.AST.ExpressionName)
				{
					Adoor.Expression.AST.ExpressionName en = (Adoor.Expression.AST.ExpressionName) selectExpression.Expr;
					this.name = en.Value;
				}
				else
					this.name = "prop" + this.sourceData.PropertyInfos.Count;
			}
			
			this.sourceData.RegisterPropertyData(this.name, this);
		}

        private IDictionary<Type, object> services = new Dictionary<Type, object>();

		public object GetService(Type type)
		{
            object result;
            if (!this.services.TryGetValue(type, out result))
                return null;
            return result;
		}

		public void RegisterService(Type type, object service)
		{
			this.services.Add(type, service);
		}

		public string Name
		{
			get { return this.name; }
		}

		public EntityData SourceData
		{
			get { return this.sourceData; }
		}

		public PropertyInfo PropertyInfo
		{
			get { return this.propertyInfo; }
		}

		public Adoor.Expression.AST.SelectExpression SelectExpression
		{
			get{ return selectExpression; }
		}
	}
}